<template>
  <div>
    <webHead />
    <div class="personal">
      <div class="form-box">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              :class="{ border: form.avatar }"
              action="http://192.168.4.105:7777/e5/file/upload"
              :show-file-list="false"
              :on-change="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="form.avatar" :src="form.avatar" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" />
              <!-- <div v-if="showText" class="avatar-text">
                <span> 更换头像 </span>
              </div> -->
            </el-upload>
          </el-form-item>
          <el-form-item label="账号">
            <el-input v-model="form.username" disabled />
          </el-form-item>
          <el-form-item label="名称">
            <el-input v-model="form.nickname" />
          </el-form-item>
          <el-form-item label="性别">
            <el-radio-group v-model="form.sex">
              <el-radio label="男" />
              <el-radio label="女" />
            </el-radio-group>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input v-model="form.age" />
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="form.phone" />
          </el-form-item>
          <el-form-item label="所在地">
            <el-select
              v-model="form.prov"
              placeholder="请选择省份"
              @change="getProv"
            >
              <el-option
                v-for="(item, i) in provs"
                :key="i"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <span style="margin: 0 10px; color: #999">-</span>
            <el-select v-model="form.city" placeholder="请选择城市">
              <el-option
                v-for="(item, i) in citys"
                :key="i"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="个性签名">
            <el-input v-model="form.gx" rows="3" type="textarea" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">保存</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import webHead from '../web/components/head.vue'
import { mapGetters } from 'vuex'
import dataset from './data.js'
export default {
  name: 'WebPersonal',
  components: {
    webHead
  },
  data() {
    return {
      form: {
        username: '',
        nickname: '',
        sex: '',
        age: '',
        phone: '',
        gx: '',
        avatar: '',
        prov: '',
        city: ''
      },
      provs: [],
      citys: [],
      showText: true
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  mounted() {
    if (this.form.prov) {
      const result = []
      const { citys } = dataset
      citys.forEach((item) => {
        if (item.prov === this.form.prov) {
          result.push({ value: item.label, label: item.label })
        }
      })
      this.citys = result
    }

    // console.log(getUsers());
  },
  created() {
    this.getUserInfo()
    this.provs = dataset.provs
  },
  methods: {
    async logout() {
      await this.$localStorage.removeItem('curUser')
      this.$router.push(`/login`)
    },
    async onSubmit() {
      const allAccount = await this.$localStorage.getItem('allAccount')

      const newAccount = allAccount.map((item) => {
        if (item.username === this.form.username) {
          return this.form
        }
        return item
      })
      await this.$localStorage.setItem('allAccount', newAccount)
      this.$store.dispatch('user/getInfo', Object.assign({}, this.form))
      this.$message.success('修改成功')
    },
    handleAvatarSuccess(file) {
      // this.form.avatar = URL.createObjectURL(file.raw)
      const fd = new FileReader()
      fd.readAsDataURL(file.raw)
      // 将文件转化为base64格式写入json文件
      fd.onload = () => {
        const keyString = fd.result
          .toString()
          .replace('data:application/x-pkcs12;base64,', '')
        // console.log("keyString", keyString)
        this.form.avatar = keyString
      }
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isLt2M
    },
    getProv: function(prov) {
      const tempCity = []
      this.citys = []
      this.form.city = ''
      for (var val of dataset.citys) {
        if (prov === val.prov) {
          tempCity.push({ label: val.label, value: val.label })
        }
      }
      this.citys = tempCity
    },
    getCity: function(city) {
      console.log(city)
      console.log(this.selectCity)
    },

    getUserInfo() {
      this.form = Object.assign({}, this.userInfo)
    }
  }
}
</script>

  <style lang="scss" scoped>
.header form,
.header h3,
.header input,
.header li,
.header ul {
  padding: 0;
  margin: 0;
  list-style: none;
  border: none;
}

.header {
  position: fixed;
  top: 0;
  z-index: 999;
  width: 100%;
  min-width: 1200px;
  background-color: #fff;
  border-bottom: 1px solid #d8d8d8;
}

.header .header-inner {
  width: 1200px;
  margin: 0 auto;
  height: 80px;
}

.header-placeholder {
  height: 81px;
}

.header .logo {
  background: url(../../icons/image/logo.png) no-repeat 0;
  background-size: contain;
  overflow: hidden;
  margin-right: 20px;
}

.header .logo,
.header a.logo {
  float: left;
  width: 133px;
  height: 60px;
  margin-top: 10px;
}

.header a.logo {
  line-height: 100em;
}

.header .nav {
  background-color: #fff;
  overflow: hidden;
  width: 530px;
  float: left;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  -webkit-flex-direction: row;
  -ms-flex-direction: row;
  flex-direction: row;
  -webkit-box-pack: justify;
  -webkit-justify-content: space-between;
  -ms-flex-pack: justify;
  justify-content: space-between;
}

.header .nav div {
  float: left;
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  -ms-flex: 1;
  flex: 1;
}
.more {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  -webkit-flex-direction: row;
  -ms-flex-direction: row;
  flex-direction: row;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
}

.more:hover {
  border-color: #d8d8d8;
  border-bottom-color: #fff;
  border-left: 0.1px solid #dcd4d7;
  border-right: 0.1px solid #dcd4d7;
}

.more:hover .configlist {
  display: block;
}

.more:hover .caret {
  -webkit-transform: rotate(180deg);
  -ms-transform: rotate(180deg);
  transform: rotate(180deg);
}

.more:hover:after {
  content: " ";
  position: absolute;
  bottom: -1px;
  left: 1px;
  right: 1px;
  background-color: #fff;
  z-index: 200;
}

.more .apptext {
  float: left;
  display: inline-block;
  font-size: 18px;
  color: #000;
  height: 24px;
  line-height: 24px;
  margin: 0 7px;
}

.more .caret {
  float: left;
  position: relative;
}

.more .configlist {
  display: none;
  width: 66px;
  position: absolute;
  top: 80px;
  background-color: #fff;
  border: 0.1px solid #dcd4d7;
  border-top: 0;
}

.more .configlist .config-item {
  color: #000;
  font-size: 18px !important;
  height: 0 !important;
  line-height: 35px !important;
  height: 35px !important;
}

.header .nav div a {
  text-align: center;
  display: inline-block;
  height: 80px;
  line-height: 80px;
  width: 100%;
  font-size: 18px;
  color: #333;
}

.header .nav div a:hover {
  color: #ef4238;
}

.header .nav div a.active {
  color: #fff;
  background-color: #ef4238;
}

.header form {
  float: right;
  margin: 20px 10px 0 0;
  position: relative;
}

.header form input {
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

.header form .search {
  display: inline-block;
  height: 40px;
  line-height: 1.2;
  width: 220px;
  padding: 0 40px 0 20px;
  border: 1px solid #ccc;
  font-size: 14px;
  border-radius: 30px;
  background-color: #faf8fa;
  overflow: hidden;
  color: #333;
  outline: none;
}

.ie8 .header form .search {
  padding-top: 11px;
}

.header form .submit {
  display: inline-block;
  position: absolute;
  left: 180px;
  top: 0;
  height: 40px;
  width: 40px;
  background-color: #ef4238;
  border-radius: 30px;
  background-image: url();
  cursor: pointer;
}

.header .user-info {
  float: right;
  position: relative;
  z-index: 9999;
  height: 100%;
}

.header .user-info .user-avatar {
  display: block;
  border: 1px solid transparent;
  border-top: none;
  border-bottom: none;
  padding: 0 10px;
  width: 56px;
  height: 100%;
}

.header .user-info .user-avatar img {
  margin-top: 20px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
}

.header .user-info .user-avatar .caret {
  position: absolute;
  top: 39px;
  right: 10px;
}

// .header .user-info .user-avatar:hover {
//   border-color: #d8d8d8;
// }

// .header .user-info .user-avatar:hover .caret {
//   -webkit-transform: rotate(180deg);
//   -ms-transform: rotate(180deg);
//   transform: rotate(180deg);
// }

.header .user-info .user-avatar:hover .user-menu {
  display: block;
}

// .header .user-info .user-avatar:hover:after {
//   content: " ";
//   position: absolute;
//   height: 10px;
//   bottom: -1px;
//   left: 1px;
//   right: 1px;
//   background-color: #fff;
// }

.header .user-info .user-menu {
  display: none;
  position: absolute;
  right: 0;
  top: 80px;
  border: 1px solid #d8d8d8;
  background-color: #fff;
  font-size: 14px;
  color: #333;
  text-align: right;
  padding: 15px 10px 5px;
  text-align: center;
}

.header .user-info .user-menu li {
  margin-bottom: 6px;
}

.header .user-info .user-menu li > a {
  color: #999;
  display: block;
  word-break: keep-all;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header .user-info .user-menu li > a:hover {
  color: #ef4238;
  text-decoration: underline;
}

.header .user-info .user-menu li.text {
  word-break: keep-all;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

.header .user-info .no-login-menu {
  padding: 15px 24px 5px;
}
.avatar-uploader ::v-deep .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 30px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader.border ::v-deep .el-upload {
  border-color: transparent;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}
.avatar {
  width: 128px;
  height: 128px;
  border-radius: 30px;
  display: block;
  // object-fit: cover;
}
.avatar-text {
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  span {
    color: #fff;
  }
}

.personal {
  display: flex;
  justify-content: center;
  padding: 140px 100px 0;
  .form-box {
    max-width: 700px;
    width: 100%;
  }
}
</style>
